package api

import (
	"net/http"

	"github.com/gorilla/mux"
)

const (
	RVERSION = "/v1/"
)

var mRouter = map[string]map[string]func(http.ResponseWriter, *http.Request){
	"GET": {
		"start_node/{node_id}":                   startNodeHandler,
		"stop_node/{node_id}":                    stopNodeHandler,
		"restart_node/{node_id}":                 restartNodeHandler,
		"pause_node/{node_id}":                   pauseNodeHandler,
		"resumeNodeHandler":                      resumeNodeHandler,
		"add_node/{node_id}":                     addNodeHandler,
		"remove_node/{node_id}":                  removeNodeHandler,
		"propose_node/{node_id}/{data}":          proposeNodeHandler,
		"dump/all":                               dumpAllNodeHandler,
		"add_peers/{id}/{content}":               addPeersHandler,
		
		"start_m/{id}":		                     startMultiNodeHandler,
		"stop_m/{id}":                            stopMultiNodeHandler,
		"dump_m/all/{group_id}":                  dumpAllMultiNodeHandler,
		"propose_m/{group_id}/{data}":            proposeMultiNodeHandler,
		"remove_group/{id}/{group_id}":           removeGroupHandler,
		"create_group/{id}/{group_id}/{peer_id}": createGroupHandler,
		"snapshot":                               snopshotAppendHandler,
		"add_mnode/{id}/{group_id}/{node_id}":    addMultiNodeHandler,
		"snapshot_m/{group_id}":                  snapshotMultiNodeHandler,
	},

	"POST": {
	//"create_group/{id}/{group_id}/{peer_id}": createGroupHandler,
	},
}

func HandlerRouter() http.Handler {
	router := mux.NewRouter()

	for method, routes := range mRouter {
		for route, fct := range routes {
			localRoute := RVERSION + route
			localMethod := method
			handler := handlerRequest(fct)
			router.Path(localRoute).Methods(localMethod).HandlerFunc(handler)
		}
	}

	router.NotFoundHandler = http.NotFoundHandler()
	return router
}

func handlerRequest(handlerFunc func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		handlerFunc(w, r)
	}
}
